package com.atguigu.lease.common.result;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {
    private Integer code;
    private String message;
    private T data;
    private Boolean ok;

    public static <T> Result<T> build(T data, ResultCodeEnum resultCodeEnum) {
        return new Result<>(resultCodeEnum.getCode(),
                resultCodeEnum.getMessage(),
                data,
                resultCodeEnum.getOk());
    }

    public static <T> Result<T> build(T data, Integer code, String message, Boolean ok) {
        return new Result<>(code,
                message,
                data,
                ok);
    }

    //成功的响应 创建成功Result对象的方法
    public static <T> Result<T> ok(T data) {
        return Result.build(data, ResultCodeEnum.SUCCESS);
    }

    public static <T> Result<T> ok() {
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    //失败的响应 创建默认失败Result对象的方法
    public static <T> Result<T> error() {
        return Result.build(null, ResultCodeEnum.ERROR);
    }

    public static <T> Result<T> error(ResultCodeEnum resultCodeEnum) {
        return Result.build(null, resultCodeEnum);
    }
}
